<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/11 0011
 * Time: 20:31
 */

namespace api\storage\model;


use service\excel\feed\feedSeat;
use think\Model;

class MachineSeatModel extends Model
{

    protected $table = "smt_seat_machine";
    protected $pk = "uuid";

    /***
     * @param $where
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 根据条件寻找机器信息
     */
    public function findMachineSeatInfo($where)
    {
        $result = $this
            ->where($where)
            ->find();
        return $result;
    }

    /***
     * @param $where
     * @param $update
     * 更新机台站位信息
     * @return MachineSeatModel
     */
    public function updateMachineSeat($where,$update){
        $result = $this
            ->where($where)
            ->update($update);
        return $result;
    }

    /***+
     * @param $where
     * @param string $field
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 根据条件查询站位
     */
    public function selectMachineSeat($where,$field = '*'){
        $result = $this->where($where)
            ->field($field)
            ->select();
        return $result;
    }

    /***
     * @param $machineId
     * @param $seatCode
     * @param $update
     * @return MachineSeatModel
     * 根据机器Id和机器站位号对机器进行更新
     *
     */
    public function updateByMachineIdAndSeat($machineId,$seatCode,$update){
        $where['m.machine_id'] = $machineId;
        $where['ms.seat_code'] = $seatCode;
        $result = $this
            ->alias('ms')
            ->join(['smt_machine' => 'm'],'ms.machine_uuid = m.uuid')
            ->where($where)
            ->update($update);
        return $result;
    }

    /**
     * @param $machineUuid
     * @return int|string
     * @throws \think\Exception
     */
    public function checkMachineStatus($machineUuid){
        $result = $this->where(['machine_uuid' => $machineUuid,'is_used'=> 1])->count();
        return $result;
    }
//    public function getFeedDetailByMachineReel($subMaterielId,$storageUuid){
//        $where['machineSeat.sub_materiel_id'] = $subMaterielId;
//        $where['feed.storage_uuid'] = $storageUuid;
//        $where['machineSeat.is_used'] = 1;
//        $result = $this->alias('machineSeat')
//                        ->join(['smt_machine' => 'machine'],'machine.uuid = machineSeat.machine_uuid','inner')
//                        ->join(['smt_feed_station' => 'feed'],'feed.uuid = machine.feed_uuid','inner')
//                        ->join(['smt_feed_station_detail' => 'feedDetail'],'feedDetail.feed_id = feed.feed_id','inner')
//
//                        ->where($where)
//                        ->where('feedDetail.sent_count > feedDetail.total_count')
//                        ->field('feedDetail.feed_id feedId,feedDetail.total_count,machine.machine_id machineId, machineSeat.seat_code machineSeat,feedDetail.sent_count sent_count')
//
//                        ->select();
//        return $result;
//    }



}